<?php

namespace App\Models\order;

use App\Models\BaseModel;
use App\Models\users\UsersModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use lxs\tools\Tools;

class OrderModel extends BaseModel
{
    protected $table = 'order';
    protected $primaryKey = 'id';

    public static function getOrderStatusValues($key = null): array|false|int|string
    {
        $values = [
            '0' => '待支付',
            '1' => '已支付',
            '2' => '已完成',
            '3' => '无效订单',
            '4' => '已退款',
        ];
        if ( is_numeric($key) && $key > 0 && $key <= 6 ) {
            return $values[$key];
        }
        if (is_string($key) && $key != '') {
            return array_search($key, $values);
        }
        return $values;
    }

    protected function payTime(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => Tools::timeConvert($value)
        );
    }

    protected function createTime(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => Tools::timeConvert($value)
        );
    }

    protected function updateTime(): Attribute
    {
        return Attribute::make(
            get: fn (string $value) => Tools::timeConvert($value)
        );
    }


    public function userInfo(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(UsersModel::class, 'uid', 'id');
    }


}
